#include "GameApplication.h"
#include "Avatar.h"
#include "Library.h"
#include "UpdateTask.h"
#include "CombatComponent.h"
#include "StatusComponent.h"
#include "RigidBodyComponent.h"
#include "SelectableComponent.h"
#include "OrientationComponent.h"
#include "GraphicalComponent.h"
#include "EntityController.h"

bool GameApplication::CreateEntity( HashedString avatarName, Pointer<Entity>& entity )
{
	Pointer<IResource> resource = Library.getSingleton().getResource(Avatar::resourceType, avatarName);

	if (!resource.isValid())
		return false;					// cannot find avatar


	entity = new Entity();

	Pointer<Avatar> avatar;
	CopyPointer(resource, avatar);

	Avatar::Attribute::Map_t& attributes = avatar->Attributes();

	for (Avatar::Attribute::Map_t::iterator it = attributes.begin(); it != attributes.end(); ++it)
	{
		const HashedString attribType(it->first);
		std::map<HashedString, HashedString>& attribMap = it->second.attributes;
		
		if (attribType == CombatComponent::type)
		{
			for (std::map<HashedString, HashedString>::iterator it2 = attribMap.begin(); it2 != attribMap.end(); ++it2)
			{
				it2->second
			}
		} else if (attribType == StatusComponent::type)
		{
			for (std::map<HashedString, HashedString>::iterator it2 = attribMap.begin(); it2 != attribMap.end(); ++it2)
			{
				it2->second
			}
		} else if (attribType == OrientationComponent::type)
		{
			for (std::map<HashedString, HashedString>::iterator it2 = attribMap.begin(); it2 != attribMap.end(); ++it2)
			{
				it2->second
			}
		} else if (attribType == RigidBodyComponent::type)
		{
			for (std::map<HashedString, HashedString>::iterator it2 = attribMap.begin(); it2 != attribMap.end(); ++it2)
			{
				it2->second
			}
		} else if (attribType == SelectableComponent::type)
		{
			for (std::map<HashedString, HashedString>::iterator it2 = attribMap.begin(); it2 != attribMap.end(); ++it2)
			{
				it2->second
			}
		} else if (attribType == GraphicalComponent::type)
		{
			for (std::map<HashedString, HashedString>::iterator it2 = attribMap.begin(); it2 != attribMap.end(); ++it2)
			{
				it2->second
			}
		} else if (attribType == HashedString("EntityController"))
		{
			Pointer<EntityController> controller = new EntityController(entity);
			controller->setWorld(Game._getWorlds()["Default"]);
			m_controllers.push_back(controller);
			UpdateTask::getSingleton().RegisterUpdatable(controller);
			U dibcam
			Pointer<MinionBehavior> minion;
			Pointer<IBehavior> beh = new MinionBehavior(ai);
			CopyPointer(beh, minion);

			ai->Behavior(beh);
			ai->RegisterHandler(SetDestinationEvent::type, new CEventHandler<MinionBehavior>(minion, &MinionBehavior::Destination));
			ai->RegisterHandler(AddDestinationEvent::type, new CEventHandler<MinionBehavior>(minion, &MinionBehavior::AddDestination));
			ai->RegisterHandler(DestinationReachedEvent::type, new CEventHandler<MinionBehavior>(minion, &MinionBehavior::DestinationReached));

		}
	}

	return entity;
}